﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using MINERP.DAL;

namespace MINERP.BLL.CustomerManage
{
    public class AutoStartOrderBLL
    {
        //参数 
        private DAL.CustomerManage.AutoStartOrderDAL dal = new MINERP.DAL.CustomerManage.AutoStartOrderDAL();

        #region 指定状态的订单
        /// <summary>
        /// 指定状态的订单
        /// </summary>
        /// <param name="status"></param>
        /// <returns></returns>
        public List<OrderBagEntity> GetOrders(int status)
        {
            try
            {
                DataTable dt = dal.GetOrders(status);
                List<OrderBagEntity> orders = new List<OrderBagEntity>();
                foreach (DataRow r in dt.Rows)
                {
                    OrderBagEntity order = new OrderBagEntity();
                    order.OrderBagID = Convert.ToInt32(r["OrderBagId"]);
                    order.BagCode = r["BagCode"].ToString();
                    order.BagName = r["BagName"].ToString();
                    order.ShopName = r["ShopName"].ToString();
                    order.OrderTime = Convert.ToDateTime(r["OrderTime"]);
                    order.PreCompleteTime = Convert.ToDateTime(r["FetchTime"]);
                    order.Priority = Convert.ToInt32(r["Priority"]);
                    order.OrderBagID = Convert.ToInt32(r["OrderBagID"]);
                    order.Remark = r["Remark"].ToString();
                    orders.Add(order);
                }
                return orders;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        /// <summary>
        /// 自动开单的产品通过审核
        /// </summary>
        /// <param name="orderIds"></param>
        public void CheckAcross(List<int> orderIds)
        {
            try
            {
                int orderStatus = (int)COM.OrderBagStatus.WaitPhotoMove;
                int infoStatus = (int)COM.OrderInfoStatus.WaitPhotoMove;
                int photoStatus = (int)COM.PhotoStatus.WaitPhotoMove;
                int oldOrderStatus = (int)COM.OrderBagStatus.AutoStartOrder;
                int oldInfoStatus = (int)COM.OrderInfoStatus.AutoStartOrder;
                int oldPhotoStatus = (int)COM.PhotoStatus.AutoStartOrder;
                dal.CheckAcross(orderIds, orderStatus, infoStatus, photoStatus, oldOrderStatus, oldInfoStatus, oldPhotoStatus);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 删除指定的订单
        /// </summary>
        /// <param name="orderIds"></param>
        public void DelOrder(List<int> orderIds)
        {
            try
            {
                int delStatus = (int)COM.OrderBagStatus.Deleted;
                int delPhoto = (int)COM.PhotoStatus.OrderDeleted;
                int delOrderInfo = (int)COM.OrderInfoStatus.Deleted;
                foreach (int orderBagId in orderIds)
                {
                    Tb_OrderBag order = dal.GetOrderById(orderBagId);
                    if (!order.orderInfo.IsLoaded)
                    {
                        order.orderInfo.Load(System.Data.Objects.MergeOption.OverwriteChanges);
                    }
                    List<orderInfo> infos = order.orderInfo.ToList();
                    List<photoInfo> photos = new List<photoInfo>();
                    foreach (orderInfo info in infos)
                    {
                        if (!info.photoInfo.IsLoaded)
                        {
                            info.photoInfo.Load(System.Data.Objects.MergeOption.OverwriteChanges);
                        }
                        info.status = delOrderInfo;
                        photos.AddRange(info.photoInfo.ToList());
                    }
                    order.Status = delStatus;
                    foreach (photoInfo photo in photos)
                    {
                        photo.status = delPhoto;
                    }
                }
                dal.Save();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
